load("@rules_rust//rust:defs.bzl", "rust_library")
load("//rs/tests:common.bzl", "MESSAGE_CANISTER_ENV", "MESSAGE_CANISTER_RUNTIME_DEPS", "UNIVERSAL_VM_RUNTIME_DEPS")
load("//rs/tests:system_tests.bzl", "system_test_nns")

package(default_visibility = ["//rs:system-tests-pkg"])

SUBNET_RECOVERY_RUNTIME_DEPS = [
    # Keep sorted.
    "//rs/tests:recovery/binaries",
]

ORCHESTRATOR_UNIVERSAL_VM_ACTIVATION = [":orchestrator_universal_vm_activation.sh"]

STATIC_FILE_SERVER_IMAGE_RUNTIME_DEPS = [
    # Keep sorted.
    "//rs/tests:static-file-server.tar",
]

rust_library(
    name = "lib",
    testonly = True,
    srcs = glob(["**/*.rs"]),
    crate_name = "ic_consensus_system_test_subnet_recovery",
    deps = [
        # Keep sorted.
        "//rs/nns/constants",
        "//rs/protobuf",
        "//rs/recovery",
        "//rs/registry/subnet_features",
        "//rs/registry/subnet_type",
        "//rs/rust_canisters/canister_test",
        "//rs/tests/consensus/tecdsa/utils",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/types/base_types",
        "//rs/types/management_canister_types",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:candid",
        "@crate_index//:prost",
        "@crate_index//:serde",
        "@crate_index//:serde_json",
        "@crate_index//:slog",
        "@crate_index//:ssh2",
        "@crate_index//:url",
    ],
)

system_test_nns(
    name = "sr_app_same_nodes_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "colocate",
        "long_test",  # since it takes longer than 5 minutes.
        "subnet_recovery",
    ],
    test_timeout = "eternal",
    uses_guestos_test_update = True,
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS + SUBNET_RECOVERY_RUNTIME_DEPS,
    deps = [
        ":lib",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "sr_app_same_nodes_enable_chain_keys_test",
    env = MESSAGE_CANISTER_ENV,
    flaky = True,  # flakiness rate of over 3.33% over the month from 2025-02-11 till 2025-03-11.
    tags = [
        "colocate",
        "long_test",
        "subnet_recovery",
    ],
    test_timeout = "eternal",
    uses_guestos_test_update = True,
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS + SUBNET_RECOVERY_RUNTIME_DEPS,
    deps = [
        ":lib",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "sr_app_same_nodes_with_chain_keys_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "colocate",
        "long_test",
        "subnet_recovery",
    ],
    test_timeout = "eternal",
    uses_guestos_test_update = True,
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS + SUBNET_RECOVERY_RUNTIME_DEPS,
    deps = [
        ":lib",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "sr_app_failover_nodes_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "colocate",
        "long_test",  # since it takes longer than 5 minutes.
        "subnet_recovery",
    ],
    test_timeout = "eternal",
    uses_guestos_test_update = True,
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS + SUBNET_RECOVERY_RUNTIME_DEPS,
    deps = [
        ":lib",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "sr_app_failover_nodes_enable_chain_keys_test",
    env = MESSAGE_CANISTER_ENV,
    flaky = True,  # flakiness rate of over 1.67% over the month from 2025-02-11 till 2025-03-11.
    tags = [
        "colocate",
        "long_test",
        "subnet_recovery",
    ],
    test_timeout = "eternal",
    uses_guestos_img = True,
    uses_guestos_test_update = True,
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS + SUBNET_RECOVERY_RUNTIME_DEPS,
    deps = [
        ":lib",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "sr_app_failover_nodes_with_chain_keys_test",
    env = MESSAGE_CANISTER_ENV,
    flaky = True,  # flakiness rate of 3.33% over the month from 2025-02-11 till 2025-03-11.
    tags = [
        "colocate",
        "long_test",
        "subnet_recovery",
    ],
    test_timeout = "eternal",
    uses_guestos_img = True,
    uses_guestos_test_update = True,
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS + SUBNET_RECOVERY_RUNTIME_DEPS,
    deps = [
        ":lib",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "sr_app_no_upgrade_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "colocate",
        "long_test",  # since it takes longer than 5 minutes.
        "subnet_recovery",
    ],
    test_timeout = "eternal",
    uses_guestos_test_update = True,
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS + SUBNET_RECOVERY_RUNTIME_DEPS,
    deps = [
        ":lib",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "sr_app_no_upgrade_local_test",
    env = MESSAGE_CANISTER_ENV | {
        "IC_ADMIN_PATH": "$(rootpath //rs/registry/admin:ic-admin)",
    },
    tags = [
        "colocate",
        "long_test",  # since it takes longer than 5 minutes.
        "subnet_recovery",
    ],
    test_timeout = "eternal",
    uses_guestos_test_update = True,
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS + [
        "//rs/registry/admin:ic-admin",
    ],
    deps = [
        ":lib",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "sr_app_no_upgrade_enable_chain_keys_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "colocate",
        "long_test",
        "subnet_recovery",
    ],
    test_timeout = "eternal",
    uses_guestos_img = True,
    uses_guestos_test_update = True,
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS + SUBNET_RECOVERY_RUNTIME_DEPS,
    deps = [
        ":lib",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "sr_app_no_upgrade_with_chain_keys_test",
    env = MESSAGE_CANISTER_ENV,
    flaky = True,  # flakiness rate of over 6% over the month from 2025-02-11 till 2025-03-11 (only for //rs/tests/consensus/subnet_recovery:sr_app_no_upgrade_with_chain_keys_test_head_nns_colocate).
    tags = [
        "colocate",
        "long_test",
        "subnet_recovery",
    ],
    test_timeout = "eternal",
    uses_guestos_img = True,
    uses_guestos_test_update = True,
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS + SUBNET_RECOVERY_RUNTIME_DEPS,
    deps = [
        ":lib",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "sr_app_large_with_chain_keys_test",
    enable_head_nns_variant = False,  # Let's not run this expensive test against the HEAD NNS canisters to save resources.
    env = MESSAGE_CANISTER_ENV,
    flaky = True,  # flakiness rate of over 2.21% over the month from 2025-02-11 till 2025-03-11.
    tags = [
        "colocate",
        "subnet_recovery",
        "system_test_large",  # only run as part of release-testing since this test requires many resources.
    ],
    test_timeout = "eternal",
    uses_guestos_img = True,
    uses_guestos_test_update = True,
    runtime_deps = MESSAGE_CANISTER_RUNTIME_DEPS + SUBNET_RECOVERY_RUNTIME_DEPS,
    deps = [
        ":lib",
        "//rs/tests/driver:ic-system-test-driver",
        "@crate_index//:anyhow",
    ],
)

system_test_nns(
    name = "sr_nns_failover_nodes_test",
    env = MESSAGE_CANISTER_ENV,
    tags = [
        "colocate",
        "long_test",  # since it takes longer than 5 minutes.
        "subnet_recovery",
    ],
    test_timeout = "eternal",
    runtime_deps =
        MESSAGE_CANISTER_RUNTIME_DEPS +
        UNIVERSAL_VM_RUNTIME_DEPS +
        SUBNET_RECOVERY_RUNTIME_DEPS + ORCHESTRATOR_UNIVERSAL_VM_ACTIVATION + STATIC_FILE_SERVER_IMAGE_RUNTIME_DEPS,
    deps = [
        # Keep sorted.
        ":lib",
        "//rs/recovery",
        "//rs/registry/subnet_type",
        "//rs/tests/consensus/utils",
        "//rs/tests/driver:ic-system-test-driver",
        "//rs/tests/networking/canister_http",
        "//rs/types/types",
        "@crate_index//:anyhow",
        "@crate_index//:ic-agent",
        "@crate_index//:slog",
        "@crate_index//:url",
    ],
)

exports_files([
    "orchestrator_universal_vm_activation.sh",
])
